<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

/**
 * 字典
 *
 * @Desc:验证请求类
 *
 * @author: zhx
 *
 * @Time: 2023/8/10 10:37
 */
class SysMapRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 获取应用于请求的验证规则。
     *
     * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
     */
    public function rules()
    {
        return [
            'map_code' => 'required|string|max:150',
            'parent_val' => 'required|int',
            'map_name' => 'required|string|max:500',
            'map_val' => '',
            'map_val2' => '',
            'map_val3' => '',
            'sort' => 'required|int',
            'map_desc' => 'required|string|max:255',
        ];
    }

    public function messages(): array
    {
        return [
            'map_code.required' => '字典编码是必填字段。',
            'map_code.max' => '字典编码不能超过 150 个字符。',
            'map_name.max' => '字典标题不能超过 500 个字符。',
            'map_name.required' => '字典类型是必填字段。',
            'parent_val.required' => '父级是必填字段。',
            'sort.required' => '排序是必填字段。',
            'map_desc.required' => '字典描述是必填字段。',
            'map_desc.max' => '字典描述不能超过 500 个字符。',
        ];
    }

    public function all($keys = null)
    {
        $data = parent::all($keys);

        return $data;
    }

    protected function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->all();
        throw new HttpResponseException(response()->json(['msg' => $error[0], 'code' => '500', 'data' => 'error'], 500));
    }
}
